<template>
  <div class="body">
    
    <div class="register">
       <el-form ref="form" :model="form" label-width="80px" :rules="rules" 
       v-loading="loading"  status-icon  >
        <div id="back">
           <el-button icon="el-icon-back" round type="primary" >
          <router-link to="/login">返回</router-link>
        </el-button>
        </div>
       
       
        
      <el-form-item label="用户名" prop="username">
       <el-input v-model="form.username" placeholder="请输入用户名" prefix-icon="el-icon-user-solid"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password" placeholder="请输入密码" prefix-icon="el-icon-lock"> </el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="password2">
        <el-input type="password" v-model="form.password2" placeholder="请确认密码" prefix-icon="el-icon-lock" > </el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱" prefix-icon="el-icon-message"> </el-input>
      </el-form-item>

      <!-- <el-form-item label="电话" prop="phone">
        <el-input  v-model="form.phone" placeholder="请输入电话" prefix-icon="el-icon-phone"> </el-input>
      </el-form-item> -->

      <el-form-item >
        
         <el-button type="primary" id="login-button" size="big" @click="register" class="regbutton" 
          :disabled="disbutton">注 册</el-button>
     </el-form-item>
    </el-form>
     
    </div>
   

  </div>
</template>

<script>
import reqLogin  from '@/api/login/login'
// import { startSakura } from '../../utils/js/fullScreenFlower'
export default {
  // mountd(){
  //   //startSakura()
  // },
    data(){
      var validateUser = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'));
        } else if (!this.regex_username.test(value)) {
          callback(new Error('用户名必须是字母数字的组合6-16位'));
        } else {
          callback();
        }
      }; 



      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else if (!this.regex_password.test(value)) {
          callback(new Error('密码必须是字母数字的组合6-16位'));
        } else {
          callback();
        }
      }; 


      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.form.password) {
          callback(new Error('两次输入密码不一致'));
        } else {
          callback();
        }
      };
      var validateEmail = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入邮箱'));
        } else if (!this.regex_email.test(value)) {
          callback(new Error('邮箱格式不正确'));
        } else {
          callback();
        }
      };



    return{
        regex_username:/^[a-zA-Z0-9_-]{6,16}$/,
        regex_password:/^[a-zA-Z0-9_-]{6,16}$/,
        regex_email:/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,

        form:{
           username:'',
           password:'',  
           password2:'',    
           email:'',         
        },
       
        rules:{
            username:[
                {validator:validateUser, trigger: 'blur'}
            ],
            password:[
                {validator:validatePass,  trigger: 'blur'}
            ],
            password2:[
                 {validator:validatePass2, trigger:'blur'}
            ],
            email:[
                {validator:validateEmail, trigger: 'blur'}
            ],
          
        },
        disbutton:true,
        loading:false
    }
  },
  methods:{
    async  register(){
                this.loading = true

          const res = await reqLogin.register(this.form.username,this.form.password,this.form.email)
          if(res.code == 500){
          this.loading = false
                var txt =null
                var err = res.data
                  //if(err.email!='') txt = err.email
                txt =  JSON.stringify(err)
              
                this.$message(res.message+"错误："+txt)


          }if(res.message.indexOf("Failed messages:")!=-1){
          this.loading = false
              this.$message("该邮箱不正确，无法发送邮件")
          }
          if(res.code==200){
          this.loading = false
            this.$message("验证码已通过邮箱发送，请注意查收")
            //将用户信息存入store中
            const user = {
                username:this.form.username,
                password:this.form.password,
                email:this.form.email
            }

            this.$store.state.userinfo.user = user
            console.log(this.$store.state.userinfo.user);
            this.$router.push('/register2')
          }
        
     },
  },
  watch:{
      form:{
        deep:true,         
        handler(val){                     
         this.$refs['form'].validate((valid) => {
           if (valid) {
              this.disbutton = false
            } else {
              this.disbutton = true
             return false;
          }
         })        
         }
      }      
    }
  }
  

</script>

<style scoped>
 .body{
  background: url("../../assets/rgback.jpg") no-repeat center center;
  width:100%;
  height: 100%;
  background-size:cover;
  position:absolute;

 }
a{
  color: white;
  text-decoration: none;
  font-size: 16px;
  
}
 #back{
    width: 40px;
    height: 30px;
    margin-left: 10px;
 }
   .register{
    padding-top: 20px;
    width:450px;
    height: 440px;
    margin: 0 auto;
    margin-top: 100px;
    border: 1px solid rgba(11, 11, 11, 0.2);
    border-radius: 20px;
    box-shadow:  10px 10px  10px  rgba(44, 43, 43,0.4);
    background-color: rgba(255, 255, 255, 0.58);
    text-align: center;
   }
    /* #login-button{
        margin-right: 50px;

    } */
    .el-form-item{
        margin-top: 30px;
        line-height: 40px;
        width: 400px;
    }
    .regbutton{
      width: 300px;
      height: 45px;
      border-radius: 8px;
      font-size: 18px;

    }
</style>